package com.bua.control;

import java.util.ArrayList;
import java.util.HashMap;

import com.bua.net.Tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class ZZTPaint extends LinearLayout {

	private Gallery toothGallery;
	private HashMap<Integer, ZZTButton> tooths = new HashMap<Integer, ZZTButton>();

	private void CreateButton(Context context, int tag, boolean isShang,
			LinearLayout ll, OnToothClick l) {
		ZZTButton button = new ZZTButton(context, tag, isShang);
		button.setOnClickListener(l);
		this.tooths.put(tag, button);
		ll.addView(button);
	}

	public ZZTPaint(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		LinearLayout.LayoutParams tp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		this.setLayoutParams(tp);
		this.setOrientation(VERTICAL);
		LinearLayout ShangBuLayout = new LinearLayout(context);
		ShangBuLayout.setOrientation(HORIZONTAL);
		ShangBuLayout.setGravity(Gravity.CENTER_HORIZONTAL);
		LinearLayout.LayoutParams ShangBuLayoutParams=new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		ShangBuLayoutParams.setMargins(0, 150, 0, 10);
		ShangBuLayout.setLayoutParams(ShangBuLayoutParams);
		this.addView(ShangBuLayout);
		LinearLayout XiaBuLayout = new LinearLayout(context);
		XiaBuLayout.setOrientation(HORIZONTAL);
		XiaBuLayout.setGravity(Gravity.CENTER_HORIZONTAL);
		this.addView(XiaBuLayout);
		OnToothClick onToothClick = new OnToothClick();
		for (int index = 28; index >= 21; index--) {
			CreateButton(context, index, true, ShangBuLayout, onToothClick);
		}
		for (int index = 11; index <= 18; index++) {
			CreateButton(context, index, true, ShangBuLayout, onToothClick);
		}
		for (int index = 38; index >= 31; index--) {
			CreateButton(context, index, false, XiaBuLayout, onToothClick);

		}
		for (int index = 41; index <= 48; index++) {
			CreateButton(context, index, false, XiaBuLayout, onToothClick);
		}

		LinearLayout GalleryBuLayout = new LinearLayout(context);
		GalleryBuLayout.setOrientation(HORIZONTAL);
		LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		p.setMargins(0, 30, 0, 0);
		GalleryBuLayout.setLayoutParams(p);
		GalleryBuLayout.setGravity(Gravity.BOTTOM);
		toothGallery = new Gallery(context);
		toothGallery.setBackgroundColor(Color.LTGRAY);
		toothGallery.setSpacing(20);
		toothGallery.setMinimumHeight(150);
		toothGallery.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

		GalleryBuLayout.addView(toothGallery);
		this.addView(GalleryBuLayout);
	}

	class OnToothClick implements OnClickListener {

		int tag = 0;

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			tag = (Integer) v.getTag();
			toothGallery.setAdapter(new GallaryAdpter(ZZTPaint.this
					.getContext(), tag));
		}

	}

	class GallaryAdpter extends BaseAdapter {

		private Context mContext = null;
		private int mTag;
		private ArrayList<Bitmap> mImageList = new ArrayList<Bitmap>();
		private String[] mTypeList = { "n", "t", "g" };

		public GallaryAdpter(Context context, int tag) {
			super();
			mContext = context;
			mTag = tag;
			Bitmap bm = BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "gen" + String.valueOf(mTag), "png"));
			Bitmap dstBitmap = bm.copy(Config.ARGB_8888, true);
			dstBitmap.eraseColor(Color.WHITE);
			String s="";
			if(tag<30)
				s="s";
			mImageList.add(dstBitmap);
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "gen" + String.valueOf(mTag), "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "g" + String.valueOf(mTag), "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"1", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"2", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"3", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"4", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"5", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"6", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"7", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "jsg"+s+"8", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "mao"+s+"1", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "mao"+s+"2", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "mao"+s+"3", "png")));
			mImageList.add(BitmapFactory.decodeStream(Tool.getFile(mContext,
					"zzt", "mao"+s+"4", "png")));
			// mSetter = imgSetter;

//			ZZTPaint.this.toothGallery.setPadding(30, 5, 30, 5);

		}

		@Override
		public int getCount() {
			// TODO 自动生成的方法存根
			return mImageList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO 自动生成的方法存根
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO 自动生成的方法存根
			return arg0;
		}

		@Override
		public View getView(final int arg0, final View arg1, ViewGroup arg2) {
			// TODO 自动生成的方法存根
			ImageButton btnImageItem = new ImageButton(mContext);
			btnImageItem.setImageBitmap(mImageList.get(arg0));
			btnImageItem.setBackgroundColor(Color.TRANSPARENT);
			//btnImageItem.setPadding(30, 0, 30, 0);
			//LayoutParams p = new LayoutParams(
			//		LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			//p.setMargins(20, 10, 20, 10);
			//btnImageItem.setLayoutParams(p);
			btnImageItem.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO 自动生成的方法存根
					String s="";
					if(mTag<30)
						s="s";
					ZZTButton button =  ZZTPaint.this.tooths.get(mTag);;
					if (arg0 == 0)
						button.setImageSeg(0, "gen"+button.getTag());
					if (arg0 == 1)
						button.setImageSeg(2, "gen"+button.getTag());
					if (arg0 == 2)
						button.setImageSeg(4, "g"+button.getTag());
					if (arg0 > 2 && arg0 < 11)
						button.setImageSeg(1, "jsg"+s+(arg0-2));
					if (arg0 >= 11)
						button.setImageSeg(3, "mao"+s+(arg0-10));
					// GudingButton button = GudingPaint.this.tooths.get(mTag);
					// button.SetTooth(mTypeList[arg0]);
					// button.setMao(bm)
				}
			});
			return btnImageItem;
		}
	}
}
